/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.templates.L1Item;

/**
 * Class <code>RemoveCurseScroll</code> 解除咀咒的卷轴.<br>
 * 40097 - 象牙塔解咒卷轴 <br>
 * 40119 - 解除咀咒的卷轴 <br>
 * 140119 - 受祝福的 解除咀咒的卷轴 <br>
 * 140329 - 受祝福的 原住民图腾 <br>
 * 
 * @author jrwz
 * @version 2012-6-22上午09:19:10
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class RemoveCurseScroll extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new RemoveCurseScroll();
    }

    /** 解除咀咒的卷轴. */
    private RemoveCurseScroll() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        final int itemId = useItem.getItemId();

        for (final L1ItemInstance eachItem : pc.getInventory().getItems()) {
            if ((eachItem.getItem().getBless() != 2)
                    && (eachItem.getItem().getBless() != 130)) {
                continue;
            }

            // 装备中才可解除咀咒
            if (!eachItem.isEquipped()
                    && ((itemId == 40119) || (itemId == 40097))) {
                continue;
            }
            final int id_normal = eachItem.getItemId() - 200000;
            final L1Item template = ItemTable.getInstance().getTemplate(
                    id_normal);
            if (template == null) {
                continue;
            }
            if (eachItem.getBless() == 130) { // 封印中的咀咒装备
                eachItem.setBless(129);
            } else { // 未封印的咀咒装备
                eachItem.setBless(1);
            }
            if (pc.getInventory().checkItem(id_normal)
                    && template.isStackable()) {
                pc.getInventory().storeItem(id_normal, eachItem.getCount());
                pc.getInventory().removeItem(eachItem, eachItem.getCount());
            } else {
                eachItem.setItem(template);
                pc.getInventory()
                        .updateItem(eachItem, L1PcInventory.COL_ITEMID);
                pc.getInventory().saveItem(eachItem, L1PcInventory.COL_ITEMID);
            }
        }
        pc.getInventory().removeItem(useItem, 1);
        pc.sendPackets(new S_ServerMessage(155)); // \f1你感觉到似乎有人正在帮助你。
    }
}
